package cn.skyquant.quant4j.api.ea;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.enums.BorkerType;
import cn.skyquant.quant4j.api.service.BaseServiceProxy;
import cn.skyquant.quant4j.sdk.enums.Status;
import com.alibaba.fastjson.JSONObject;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

public class EAServiceProxy extends BaseServiceProxy implements EAService {
    private static final String url = "/ea";

    public EAServiceProxy(String accountCode, BorkerType borkerType) {
        super(accountCode, borkerType);
    }


    @Override
    public ResultDTO<EAManagerDTO> getByKey(String ea, Integer eaId, String accountCode, BorkerType borkerType) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("ea", ea);
        paramMap.put("eaId", eaId.toString());
        paramMap.put("accountCode", accountCode);
        paramMap.put("borkerType", borkerType.name());
        JSONObject jsonObject = get(url, paramMap);
        if (jsonObject != null) {
            boolean ok = jsonObject.getBoolean("ok");
            if (!ok) {
                String message = jsonObject.getString("message");
                return ResultDTO.error(message);
            } else {
                JSONObject data = jsonObject.getJSONObject("data");
                if (data != null) {
                    try {
                        EAManagerDTO accountDTO = getEAManagerDTOFromJsonObject(data);
                        return ResultDTO.success(accountDTO);
                    } catch (Exception e) {
                        return error;
                    }
                }
            }
        }
        return error;
    }

    @Override
    public ResultDTO<EAManagerDTO> getOrCreate(String ea, Integer eaId, Integer accountId) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("ea", ea);
        paramMap.put("eaId", eaId.toString());
        paramMap.put("accountId", String.valueOf(accountId));
        JSONObject jsonObject = post(url, paramMap);if (jsonObject != null) {
            boolean ok = jsonObject.getBoolean("ok");
            if (!ok) {
                String message = jsonObject.getString("message");
                return ResultDTO.error(message);
            } else {
                JSONObject data = jsonObject.getJSONObject("data");
                if (data != null) {
                    try {
                        EAManagerDTO accountDTO = getEAManagerDTOFromJsonObject(data);
                        return ResultDTO.success(accountDTO);
                    } catch (Exception e) {
                        return error;
                    }
                }
            }
        }
        return error;
    }

    @Override
    public ResultDTO<Void> ready(Integer id, String hostname, String localIp, String remoteIp) {
        Map<String, String> paramMap = new HashMap<>();
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            paramMap.put("localIp", inetAddress.getHostAddress());
            paramMap.put("hostname", inetAddress.getHostName());
            JSONObject jsonObject = post(url + "/" + id + "/ready", paramMap);
            if (jsonObject != null) {
                boolean ok = jsonObject.getBoolean("ok");
                if (ok) {
                    return ResultDTO.success;
                }
            }
        } catch (UnknownHostException e) {

        }
        return ResultDTO.error;
    }

    @Override
    public ResultDTO<Void> start(Integer id) {
        JSONObject jsonObject = post(url + "/" + id + "/start", null);
        if (jsonObject != null) {
            boolean ok = jsonObject.getBoolean("ok");
            if (ok) {
                return ResultDTO.success;
            }
        }
        return ResultDTO.error;
    }

    @Override
    public ResultDTO<Void> stop(Integer id) {
        JSONObject jsonObject = post(url + "/" + id + "/stop", null);
        if (jsonObject != null) {
            boolean ok = jsonObject.getBoolean("ok");
            if (ok) {
                return ResultDTO.success;
            }
        }
        return ResultDTO.error;
    }

    @Override
    public ResultDTO<Void> heartbeat(Integer id, String borkerTime,String remoteIp) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("borkerTime", String.valueOf(borkerTime));
        JSONObject jsonObject = post(url + "/" + id + "/heartbeat", paramMap);
        if (jsonObject != null) {
            boolean ok = jsonObject.getBoolean("ok");
            if (ok) {
                return ResultDTO.success;
            }
        }
        return ResultDTO.error;
    }

    private EAManagerDTO getEAManagerDTOFromJsonObject(JSONObject data) {
        Integer id = data.getInteger("id");
        String ea = data.getString("ea");
        Integer eaId = data.getInteger("eaId");
        String eaStatus = data.getString("eaStatus");
        EAManagerDTO dto = new EAManagerDTO();
        dto.id = id;
        dto.ea = ea;
        dto.eaId = eaId;
        dto.eaStatus = Status.valueOf(eaStatus);
        return dto;
    }
}
